import tkinter as tk
from tkinter import messagebox, Canvas, Button, Label, Tk, Entry, Toplevel
import os
from PIL import Image, ImageDraw, ImageTk, ImageGrab
import numpy as np
import tensorflow as tf
credentials_file = "credentials.txt"  # File to store usernames and passwords
# Global variables
registered_users = {"20212099": "123456"}  # Already registered users

# Function to register a new user
def register_user(username, password):
    with open(credentials_file, "a") as file:
        file.write(f"{username}:{password}\n")
    registered_users[username] = password
    messagebox.showinfo("注册成功", "您已成功注册。")

# Function to open registration window
def open_registration_window():
    registration_window = Toplevel(root)
    registration_window.title("注册界面")

    # Labels and entry widgets for new username, password, and confirm password
    new_username_label = Label(registration_window, text="新用户名:")
    new_username_label.grid(row=0, column=0, padx=10, pady=5)
    new_username_entry = Entry(registration_window)
    new_username_entry.grid(row=0, column=1, padx=10, pady=5)

    new_password_label = Label(registration_window, text="新密码:")
    new_password_label.grid(row=1, column=0, padx=10, pady=5)
    new_password_entry = Entry(registration_window, show="*")
    new_password_entry.grid(row=1, column=1, padx=10, pady=5)

    confirm_password_label = Label(registration_window, text="确认密码:")
    confirm_password_label.grid(row=2, column=0, padx=10, pady=5)
    confirm_password_entry = Entry(registration_window, show="*")
    confirm_password_entry.grid(row=2, column=1, padx=10, pady=5)

    # Button to submit registration
    register_button = Button(registration_window, text="注册", command=lambda: register_new_user(
        new_username_entry.get(), new_password_entry.get(), confirm_password_entry.get()))
    register_button.grid(row=3, column=0, columnspan=2, padx=10, pady=5)

# Function to register a new user
def register_new_user(new_username, new_password, confirm_password):
    if len(new_username) < 5:
        messagebox.showerror("注册失败", "用户名长度至少应为5个字符")
        return
    
    if len(new_password) < 8:
        messagebox.showerror("注册失败", "密码长度至少应为8个字符")
        return
    
    if new_username.isalnum() and new_password.isalnum():
        if new_password == confirm_password:
            register_user(new_username, new_password)
        else:
            messagebox.showerror("注册失败", "确认密码与密码不一致。")
    else:
        messagebox.showerror("注册失败", "用户名和密码只能包含字母和数字。")

# Your existing code for authentication and digit recognition
# ...

# Main login window
root = tk.Tk()
root.title("登录界面")

# Labels and entry widgets for username and password
username_label = Label(root, text="用户名:")
username_label.grid(row=0, column=0, padx=10, pady=5)
username_entry = Entry(root)
username_entry.grid(row=0, column=1, padx=10, pady=5)

password_label = Label(root, text="密码:")
password_label.grid(row=1, column=0, padx=10, pady=5)
password_entry = Entry(root, show="*")
password_entry.grid(row=1, column=1, padx=10, pady=5)

# Buttons for login and registration
login_button = Button(root, text="登录", command=authenticate_and_run_digit_recognition)
login_button.grid(row=2, column=0, padx=10, pady=5)

register_button = Button(root, text="注册", command=open_registration_window)
register_button.grid(row=2, column=1, padx=10, pady=5)

root.mainloop()